package com.oncliedisk.administrator.DB;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.oncliedisk.administrator.content.FileConfig;
import com.oncliedisk.administrator.model.FileModel;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/3.
 */
public class Dao {
    private SqliteHelper helper;
    private static Dao dao;
    private SQLiteDatabase db;

    private Dao(Context context) {
        helper = new SqliteHelper(context);
        db = helper.getWritableDatabase();
    }

    public static Dao getInstance(Context context) {
        synchronized (Dao.class) {
            if (dao == null) {
                dao = new Dao(context);
            }
            return dao;
        }
    }

    /**
     * 获取所有目录
     *
     * @return
     */
    public List<FileModel> getAllFile() {
        List<FileModel> data = new ArrayList<>();
        FileModel model = null;
        //获取文件夹的所有第一层数据
        Cursor result = db.query("tb_folder", null, "isfirst=?", new String[]{"1"}, null, null, null);
        if (result != null) {
            while (result.moveToNext()) {
                //如果有数据
                model = new FileModel();
                //获取ID
                model.setId(result.getInt(result.getColumnIndex("_id")));
                //获取名字
                model.setName(result.getString(result.getColumnIndex("name")));
                //获取日期
                model.setCreateTime(result.getString(result.getColumnIndex("date")));
                //获取是否是第一层
                model.setIsFirst(result.getInt(result.getColumnIndex("isfirst")) == FileConfig.IS_FIRST);
                //获取上一层的文件夹ID是多少
                model.setBackId(result.getInt(result.getColumnIndex("folder_id")));
                //设置他是文件夹类型
                model.setType(FileConfig.TYPE_FOLDER);
                data.add(model);
            }
        }
        //获取所有第一层的所有文件
        Cursor cursor = db.query("tb_file", null, "isfirst=?", new String[]{String.valueOf(FileConfig.IS_FIRST)}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                //如果有数据
                model = new FileModel();
                model.setId(cursor.getInt(cursor.getColumnIndex("_id")));
                model.setName(cursor.getString(cursor.getColumnIndex("name")));
                model.setSize(cursor.getString(cursor.getColumnIndex("size")));
                model.setCreateTime(cursor.getString(cursor.getColumnIndex("date")));
                model.setType(cursor.getInt(cursor.getColumnIndex("type")));
                model.setPath(cursor.getString(cursor.getColumnIndex("path")));
                model.setIsFirst(cursor.getInt(cursor.getColumnIndex("isfirst")) == FileConfig.IS_FIRST);
                model.setBackId(cursor.getInt(cursor.getColumnIndex("folder_id")));
                data.add(model);
            }
        }
        return data;
    }

    /**
     * 根据ID来获取数据
     *
     * @param id
     * @return
     */
    public List<FileModel> getFileById(int id) {
        List<FileModel> data = new ArrayList<>();
        FileModel model = null;
        //获取文件夹的所有第一层数据
        Cursor result = db.query("tb_folder", null, "folder_id=?", new String[]{String.valueOf(id)}, null, null, null);
        if (result != null) {
            while (result.moveToNext()) {
                //如果有数据
                model = new FileModel();
                //获取ID
                model.setId(result.getInt(result.getColumnIndex("_id")));
                //获取名字
                model.setName(result.getString(result.getColumnIndex("name")));
                //获取日期
                model.setCreateTime(result.getString(result.getColumnIndex("date")));
                //获取是否是第一层
                model.setIsFirst(result.getInt(result.getColumnIndex("isfirst")) == FileConfig.IS_FIRST);
                //获取上一层的文件夹ID是多少
                model.setBackId(result.getInt(result.getColumnIndex("folder_id")));
                //设置他是文件夹类型
                model.setType(FileConfig.TYPE_FOLDER);
                data.add(model);
            }
        }
        //获取所有第一层的所有文件
        Cursor cursor = db.query("tb_file", null, "folder_id=?", new String[]{String.valueOf(id)}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                //如果有数据
                model = new FileModel();
                model.setId(cursor.getInt(cursor.getColumnIndex("_id")));
                model.setName(cursor.getString(cursor.getColumnIndex("name")));
                model.setSize(cursor.getString(cursor.getColumnIndex("size")));
                model.setCreateTime(cursor.getString(cursor.getColumnIndex("date")));
                model.setType(cursor.getInt(cursor.getColumnIndex("type")));
                model.setPath(cursor.getString(cursor.getColumnIndex("path")));
                model.setIsFirst(cursor.getInt(cursor.getColumnIndex("isfirst")) == FileConfig.IS_FIRST);
                model.setBackId(cursor.getInt(cursor.getColumnIndex("folder_id")));
                model.setPath(cursor.getString(cursor.getColumnIndex("path")));
                data.add(model);
            }
        }
        return data;
    }

    /**
     * 获取所有目录,通过名字
     *
     * @return
     */
    public List<FileModel> getFileByName(String name) {
        List<FileModel> data = new ArrayList<>();
        FileModel model = null;
        //获取文件夹的所有第一层数据
        Cursor result = db.query("tb_folder", null, "name like ?", new String[]{"%"+name+"%"}, null, null, null);
        if (result != null) {
            while (result.moveToNext()) {
                //如果有数据
                model = new FileModel();
                //获取ID
                model.setId(result.getInt(result.getColumnIndex("_id")));
                //获取名字
                model.setName(result.getString(result.getColumnIndex("name")));
                //获取日期
                model.setCreateTime(result.getString(result.getColumnIndex("date")));
                //获取是否是第一层
                model.setIsFirst(result.getInt(result.getColumnIndex("isfirst")) == FileConfig.IS_FIRST);
                //获取上一层的文件夹ID是多少
                model.setBackId(result.getInt(result.getColumnIndex("folder_id")));
                //设置他是文件夹类型
                model.setType(FileConfig.TYPE_FOLDER);
                data.add(model);
            }
        }
        //获取所有第一层的所有文件
        Cursor cursor = db.query("tb_file", null, "name like ?", new String[]{"%"+name+"%"}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                //如果有数据
                model = new FileModel();
                model.setId(cursor.getInt(cursor.getColumnIndex("_id")));
                model.setName(cursor.getString(cursor.getColumnIndex("name")));
                model.setSize(cursor.getString(cursor.getColumnIndex("size")));
                model.setCreateTime(cursor.getString(cursor.getColumnIndex("date")));
                model.setType(cursor.getInt(cursor.getColumnIndex("type")));
                model.setPath(cursor.getString(cursor.getColumnIndex("path")));
                model.setIsFirst(cursor.getInt(cursor.getColumnIndex("isfirst")) == FileConfig.IS_FIRST);
                model.setBackId(cursor.getInt(cursor.getColumnIndex("folder_id")));
                data.add(model);
            }
        }
        return data;
    }

    /**
     * 新建文件夹
     *
     * @param folderName 文件夹的名称
     * @param isFirst    //创建位置是否是在根目录
     * @param folder_id  //文件夹是隶属于哪个目录的ID下。
     */
    public void newFolder(String folderName, int isFirst, int folder_id) {
        ContentValues values = new ContentValues();
        values.put("name", folderName);
        values.put("date", new SimpleDateFormat("yyyy-MM-dd HH:ss:mm").format(new Date(System.currentTimeMillis())).toString());
        values.put("isfirst", isFirst);
        values.put("folder_id", folder_id);
        db.insert("tb_folder", null, values);
        Log.i("info", "插入成功");
    }

    /**
     * 删除集合里的所有文件
     * @param seleedModel
     */
    public void deleteModel(List<FileModel> seleedModel) {
        //删除本地数据：
        //根据集合中对象的类型，删除指定类型表中的数据。如果是文件夹，则删除文件夹的数据以及文件中有隶属于这个文件夹的所有文件。
        //如果是文件，则直接删除文件.
        for(FileModel model : seleedModel){
            //遍历所有数据
            if(model.getType() == FileConfig.TYPE_FOLDER){
                //如果是文件夹,则删除文件夹表的数据，以及文件表中有隶属于这个文件夹的文件
                db.delete("tb_folder","_id=?",new String[]{String.valueOf(model.getId())});
                db.delete("tb_file","folder_id=?",new String[]{String.valueOf(model.getId())});
            }else{
                db.delete("tb_file","_id=?",new String[]{String.valueOf(model.getId())});
            }
        }
        //删除网盘数据：
    }


    /**
     * 根据类型获取指定数据
     * @param typeImg
     * @return
     */
    public List<FileModel> getFileByType(int typeImg) {
        List<FileModel> data = new ArrayList<FileModel>();
        Cursor tb_file = db.query("tb_file", null, "type=?", new String[]{String.valueOf(typeImg)}, null, null, null);
        if(tb_file != null){
            while(tb_file.moveToNext()){
                int id = tb_file.getInt(tb_file.getColumnIndex("_id"));
                String date = tb_file.getString(tb_file.getColumnIndex("date"));
                String path = tb_file.getString(tb_file.getColumnIndex("path"));
                String name = tb_file.getString(tb_file.getColumnIndex("name"));
                int type = tb_file.getInt(tb_file.getColumnIndex("type"));
                FileModel model = new FileModel();
                model.setId(id);
                model.setCreateTime(date);
                model.setPath(path);
                model.setName(name);
                model.setType(type);
                data.add(model);
            }
            return data;
        }
        return data;
    }

    public void updateName(int id,int type,String name){
        ContentValues values = new ContentValues();
        values.put("name", name);
        int update = db.update(type == FileConfig.TYPE_FOLDER ? "tb_folder" : "tb_file", values, "_id=?", new String[]{String.valueOf(id)});
    }
}
